package com.lin.activity;

import java.util.ArrayList;
import java.util.List;

import com.lin.bean.PingLunBean;
import com.lin.bean.ZanList;
import com.lin.fragments.SuperFriendDynamicFragment;
import com.lin.utils.Parameters;
import com.lin.utils.Session;
import com.lin.utils.Utils;
import com.lin.utils.Session.ResultReceiver;
import com.lin.utils.Session.ZanResult;
import com.lin.views.ListViewForFriendShare.GridCommentAdapter;
import com.lin.views.ListViewForFriendShare.GridImgAdapter2;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.PinterestCircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.ShareActivity;
import com.yunfeng.supercall.bean.FriendsShareBean;
import com.yunfeng.supercall.bean.UserBean;
import com.yzx.service.MsgBackReceiver;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.internal.widget.AdapterViewCompat;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DynamicDetailActivity extends Activity implements OnItemClickListener, OnClickListener {

	private FriendsShareBean friendsShare;

	private GridView gridview;
	private ImageView imageview1;
	private ImageView imageview2;
	private ImageView imageview3;
	private TextView tvZanCount;
	private GridView listview;
	DisplayImageOptions options;
	DisplayImageOptions optionsCircle;
	private FrameLayout mFrameLayoutMore;
	private FrameLayout mFrameLayoutBack;
	EditText mEditTextAnswer;
	Session mSession;
	private UserBean userbean;
	GridCommentAdapter mGridCommentAdapter ;
	String content;

	private Button mButtonSend;

	private LinearLayout mLinearLayoutZAN;

	UserBean mUser;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		mSession = Session.getSession();
		mSession.init(getApplicationContext(), true);


		userbean = BaseActivity.getUser(getApplicationContext());
		mUser = userbean;
		setContentView(R.layout.activity_friend_share);
		mEditTextAnswer = (EditText) findViewById(R.id.et);
		ImageView ivAvatar = (ImageView) findViewById(R.id.avatar);
		TextView tvUsername = (TextView) findViewById(R.id.user_name);
		TextView tvDate = (TextView) findViewById(R.id.date);
		TextView tvContent = (TextView) findViewById(R.id.content);
		gridview = (GridView) findViewById(R.id.gridview);
		imageview1 = (ImageView) findViewById(R.id.imageview1);
		imageview2 = (ImageView) findViewById(R.id.imageview2);
		imageview3 = (ImageView) findViewById(R.id.imageview3);
		mButtonSend = (Button) findViewById(R.id.btn_send);
		tvZanCount = (TextView) findViewById(R.id.zan_count);
		listview = (GridView) findViewById(R.id.listview);
		mFrameLayoutMore = (FrameLayout) findViewById(R.id.framelayout_more);
		mFrameLayoutBack = (FrameLayout) findViewById(R.id.framelayout_back);
		mLinearLayoutZAN = (LinearLayout) findViewById(R.id.zan_area);
		mLinearLayoutZAN.setOnClickListener(this);
		mFrameLayoutBack.setOnClickListener(this);
		mFrameLayoutMore.setOnClickListener(this);
		mButtonSend.setOnClickListener(this);

		gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(DynamicDetailActivity.this, FullScreenViewActivity.class);
				List<String> mList = SuperFriendDynamicFragment.mShareBean.getPhotoList();
				if(SuperFriendDynamicFragment.mListFriendsShareBean2 != null) {
					intent.putParcelableArrayListExtra("datalist", SuperFriendDynamicFragment.mListFriendsShareBean2);
					int index = SuperFriendDynamicFragment.mListFriendsShareBean2.indexOf(SuperFriendDynamicFragment.mShareBean);
					intent.putExtra("dataitem", index);
					intent.putExtra("position", position);
					startActivity(intent);
				}
			}
		});

		mEditTextAnswer.setOnKeyListener(new View.OnKeyListener()
		{

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				// TODO Auto-generated method stub
				if(event.getAction() == KeyEvent.ACTION_DOWN)
				{
					if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_ENTER)
					{
						content = mEditTextAnswer.getText().toString();
						Utils.hidenKeyBoard(getApplicationContext(), mEditTextAnswer);
						mSession.addSharePinglun(getApplicationContext(), userbean.getUserid(), friendsShare.getShareid(), content, friendsShare.getLat(), friendsShare.getLng(), new ResultReceiver<Integer>() {

							@Override
							public void onResult(boolean last, Integer args) {
								// TODO Auto-generated method stub
								//								if(args == 1) {
								Toast.makeText(getApplicationContext(), "评论成功", Toast.LENGTH_SHORT).show();
								if(friendsShare.getJsonPinglunList() != null) { 
									friendsShare.getJsonPinglunList().add(new PingLunBean(content, "", userbean.getLat() + "", userbean.getLng() + "", userbean.getNick_name(), userbean.getPhoto(), userbean.getUserid() + ""));
								} else {
									ArrayList<PingLunBean> mList = new ArrayList<PingLunBean>();
									mList.add(new PingLunBean(content, "", userbean.getLat() + "", userbean.getLng() + "", userbean.getNick_name(), userbean.getPhoto(), userbean.getUserid() + ""));;
									friendsShare.setJsonPinglunList(mList);
								}
								mGridCommentAdapter.notifyDataSetChanged();
								//								} else {
								//									Toast.makeText(getApplicationContext(), "评论失败", Toast.LENGTH_SHORT).show();
								//								}
							}
						})  ;              
						mEditTextAnswer.setText("");
						return true;
					}
				}
				return false;
			}

		});


		options = new DisplayImageOptions.Builder()
		.displayer(new RoundedBitmapDisplayer(getResources().getDimensionPixelOffset(R.dimen.corners_avatar)))
		.showImageOnFail(R.drawable.default_avatar)
		.showImageOnLoading(R.drawable.default_avatar)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();

		optionsCircle = new DisplayImageOptions.Builder()
		/*			.showImageOnFail(R.drawable.default_avatar)
			.showImageOnLoading(R.drawable.default_avatar)*/
		.displayer(new PinterestCircleBitmapDisplayer(20))
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();

		friendsShare = SuperFriendDynamicFragment.mShareBean;

		tvContent.setText(friendsShare.getContent());
		tvZanCount.setText(friendsShare.getZannum());
		tvUsername.setText(friendsShare.getNick_name());
		tvDate.setText(friendsShare.getCreatetime());
		gridview.setAdapter(new GridImgAdapter2(getApplicationContext(), friendsShare
				.getPhotoList()));
		//			gridviewUser.setAdapter(new GridImgAdapter3(mContext, friendsShare
		//					.getPhotoList()));

		gridview.setTag(friendsShare);

		listview.setAdapter(mGridCommentAdapter = new GridCommentAdapter(getApplicationContext(), friendsShare.getJsonPinglunList()));


		ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + friendsShare.getPhoto(), ivAvatar, options);

		ArrayList<ZanList> mListZan = friendsShare.getmZanList();


		if(mListZan != null &&mListZan.size() > 0) {
			if(mListZan.size() == 1) {
				ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(0).getPhoto(), imageview1, optionsCircle);
				imageview1.setVisibility(View.VISIBLE);
				imageview2.setVisibility(View.GONE);
				imageview3.setVisibility(View.GONE);
			} else if(mListZan.size() == 2) {
				ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(0).getPhoto(), imageview2, optionsCircle);
				ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(1).getPhoto(), imageview3, optionsCircle);
				imageview1.setVisibility(View.VISIBLE);
				imageview2.setVisibility(View.VISIBLE);
				imageview3.setVisibility(View.GONE);
			} else if(mListZan.size() == 3) {
				ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(0).getPhoto(), imageview1, optionsCircle);
				ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(1).getPhoto(), imageview2, optionsCircle);
				ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mListZan.get(2).getPhoto(), imageview3, optionsCircle);
				imageview1.setVisibility(View.VISIBLE);
				imageview2.setVisibility(View.VISIBLE);
				imageview3.setVisibility(View.VISIBLE);
			}
		} else {
			imageview1.setVisibility(View.GONE);
			imageview2.setVisibility(View.GONE);
			imageview3.setVisibility(View.GONE);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(mFrameLayoutBack == view) {
			finish();
		} else if(mFrameLayoutMore == view) {
			Intent intent = new Intent(this, ShareActivity.class);
			startActivity(intent);
		} else if(mButtonSend == view) {
			content = mEditTextAnswer.getText().toString();

			mSession.addSharePinglun(getApplicationContext(), userbean.getUserid(), friendsShare.getShareid(), content, friendsShare.getLat(), friendsShare.getLng(), new ResultReceiver<Integer>() {

				@Override
				public void onResult(boolean last, Integer args) {
					// TODO Auto-generated method stub
					//							if(args == 1) {
					Utils.hidenKeyBoard(getApplicationContext(), mEditTextAnswer);
					Toast.makeText(getApplicationContext(), "评论成功", Toast.LENGTH_SHORT).show();
					if(friendsShare.getJsonPinglunList() != null) { 
						friendsShare.getJsonPinglunList().add(new PingLunBean(content, "", userbean.getLat() + "", userbean.getLng() + "", userbean.getNick_name(), userbean.getPhoto(), userbean.getUserid() + ""));
					} else {
						ArrayList<PingLunBean> mList = new ArrayList<PingLunBean>();
						mList.add(new PingLunBean(content, "", userbean.getLat() + "", userbean.getLng() + "", userbean.getNick_name(), userbean.getPhoto(), userbean.getUserid() + ""));;
						friendsShare.setJsonPinglunList(mList);
					}
					mGridCommentAdapter.notifyDataSetChanged();
					//							} else {
					//								Toast.makeText(getApplicationContext(), "评论失败", Toast.LENGTH_SHORT).show();
					//							}
				}
			})  ;              
			mEditTextAnswer.setText("");
		} else if(mLinearLayoutZAN == view) {
			mSession.addShareZan(DynamicDetailActivity.this, mUser.getUserid(), friendsShare.getShareid(), new ResultReceiver<ZanResult>() {

				@Override
				public void onResult(boolean last, ZanResult result) {
					// TODO Auto-generated method stub
					if(result == ZanResult.SUCCESS) {
						friendsShare.setZannum(Integer.parseInt(friendsShare.getZannum()) + 1 + "");

						if(friendsShare.getmZanList() == null) friendsShare.setmZanList(new ArrayList<ZanList>());
						if(!friendsShare.getmZanList().contains(new ZanList(mUser.getNick_name(), mUser.getPhoto()))) {


							try {
								friendsShare.getmZanList().add(new ZanList(mUser.getNick_name(), mUser.getPhoto()));

							} catch(NullPointerException e) {
								ArrayList<ZanList> arrayList = new ArrayList<ZanList>();
								friendsShare.setmZanList(arrayList);
								friendsShare.getmZanList().add(new ZanList(mUser.getNick_name(), mUser.getPhoto()));
							}
							tvZanCount.setText(friendsShare.getZannum() + "");
							Toast.makeText(DynamicDetailActivity.this, "点赞成功", Toast.LENGTH_SHORT).show();
						} else {
							friendsShare.getmZanList().remove(new ZanList(mUser.getNick_name(), mUser.getPhoto()));
							int num = Integer.parseInt(friendsShare.getZannum());
							friendsShare.setZannum(( num - 2) + "");
							tvZanCount.setText(friendsShare.getZannum() + "");
							Toast.makeText(DynamicDetailActivity.this, "取消成功", Toast.LENGTH_SHORT).show();
						}
					}
				}

			});
		}


	}



}